#!/usr/bin/python3
import warnings

import serial
import threading
import numpy as np
from GlobVarLock import GlobVarLocker as glob

class IMUDataRecorder(threading.Thread):
    def __init__(self, port_, baudrate_):
        threading.Thread.__init__(self)
        self.serialport = serial.Serial()
        self.serialport.port = port_
        self.serialport.baudrate = baudrate_
        self.serialport.bytesize = 8
        self.serialport.parity = serial.PARITY_NONE
        self.serialport.stopbits = 1
        self.serialport.timeout = 0.001

        if not self.serialport.is_open:
            self.serialport.open()

    def run(self):
        while (True):
            byteNum = self.serialport.inWaiting()
            if(byteNum != 0):
                byteDataFlow = self.serialport.read(byteNum)
                npDataFlow = np.frombuffer(byteDataFlow, dtype=np.uint8)
                glob.lockOriginDataRB()
                glob.putOriginDataRB(npDataFlow)
                glob.unlockOriginDataRB()
                if (glob.getOriginDataRBMaxSize() == glob.getOriginDataRBLen()):
                    print("original data buff full!")

